package com.jsgt.greekfire.ui.dialog

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.fhx.core.AppConfig
import com.fhx.core.utils.DialogUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.Version
import com.jsgt.greekfire.common.util.MD5Util
import org.jetbrains.anko.find

class DialogUtils {
    companion object {
        fun showUploadDialog(context: Context, version: Version) {
            if (AppConfig.getVersion()!! < version.version!!) {
                showUploadDialog(
                    context,
                    1,
                    version.version!!,
                    version.content!!,
                    version.url!!
                )
            } else {
                showUploadDialog(
                    context,
                    0,
                    version.version!!,
                    "",
                    ""
                )
            }
        }

        /**
         * 版本升级弹窗
         * type  0最新版本   1版本升级
         * versionName   版本号
         * content       更新内容
         */
        private fun showUploadDialog(
            context: Context,
            type: Int?,
            versionName: String,
            content: String,
            url: String
        ) {
            val contentView =
                LayoutInflater.from(context).inflate(R.layout.dialog_version_update, null, false)
            val dialog = DialogUtil.show(context, contentView)

            dialog.setCanceledOnTouchOutside(true)
            dialog.setCancelable(true)
            dialog.show()
            val txvCancel = dialog.find<TextView>(R.id.txvCancel)
            val txvOk = contentView.findViewById<TextView>(R.id.txvOk)
            val tvContent = dialog.find<TextView>(R.id.tvContent)
            val view = dialog.find<View>(R.id.view)

            if (type == 0) {
                tvContent.text = context.getString(R.string.latest_version)
                txvCancel.visibility = View.GONE
                tvContent.visibility = View.VISIBLE
                txvOk?.text = context.getString(R.string.affirm)
                view.visibility = View.GONE
            } else {
                val tvVersionName = dialog.find<TextView>(R.id.tvVersionName)
                val tvVersionContent = dialog.find<TextView>(R.id.tvVersionContent)
                if (!TextUtils.isEmpty(versionName)) {
                    tvVersionName.text = String.format(
                        context.getString(R.string.new_version_found),
                        versionName
                    )
                }
                if (!TextUtils.isEmpty(content)) {
                    tvVersionContent.text = content
                }
                txvCancel.visibility = View.VISIBLE
                view.visibility = View.VISIBLE
                tvContent.visibility = View.GONE
                txvCancel.text = context.getString(R.string.not_updated)
                txvOk?.text = context.getString(R.string.updated)
            }
            contentView.findViewById<TextView>(R.id.txvCancel).setOnClickListener {
                dialog.dismiss()
            }

            contentView.findViewById<TextView>(R.id.txvOk).setOnClickListener {
                dialog.dismiss()
                if (type == 1) {
                    val uri = Uri.parse(url)
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    context.startActivity(intent)
                    dialog.dismiss()
                }
            }
        }

        /**
         * 输入密码弹窗
         */
        fun showInputPwdDialog(
            context: Context,
            password: String?,
            okListener: (password: String) -> Unit
        ) {
            val contentView =
                LayoutInflater.from(context).inflate(R.layout.dialog_input_wallet_pwd, null, false)
            val pwdDialog = DialogUtil.show(context, contentView)
            pwdDialog.setCanceledOnTouchOutside(true)
            pwdDialog.setCancelable(true)
            pwdDialog.show()
            contentView.findViewById<TextView>(R.id.check_pwd_tv_cancel).setOnClickListener {
                pwdDialog.dismiss()
            }

            contentView.findViewById<TextView>(R.id.check_pwd_tv).setOnClickListener {
                pwdDialog.dismiss()
                val pwd = contentView.findViewById<EditText>(R.id.pwd_et).text.toString().trim()
                if (MD5Util.getMD5(pwd) == password) {
                    okListener.invoke(pwd)
                } else {
                    ToastUtils.show(R.string.pwd_error)
                }
            }
        }
    }
}